package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoodsNorms;
import com.tencreat.service.mall.IMallGoodsNormsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品规格参数Controller
 *
 * @author tencreat
 * @date 2022-01-08
 */
@Api(tags="商品规格参数api")
@RestController
@RequestMapping("/system/norms")
public class AdminMallGoodsNormsController extends BaseController {
    @Autowired
    private IMallGoodsNormsService mallGoodsNormsService;

    /**
     * 查询商品规格参数列表
     */
    @ApiOperation(value="查询商品规格参数列表")
    @PreAuthorize("@ss.hasPermi('system:norms:list,mall:store-goods:edit')")
    @PostMapping("/list")
    public HttpResult<IPage<MallGoodsNorms>> list(@RequestBody  MallGoodsNorms mallGoodsNorms) {
        QueryWrapper<MallGoodsNorms> wrapper = Wrappers.query(mallGoodsNorms);
        wrapper.orderByAsc("sort_order");
        Page<MallGoodsNorms> page =  new Page<>(mallGoodsNorms.getPageNum(), mallGoodsNorms.getPageSize());
        return HttpResult.ok(mallGoodsNormsService.page(page, wrapper));
    }
    @ApiOperation(value="查询商品规格参数列表")
//    @PreAuthorize("@ss.hasPermi('system:norms:list,mall:store-goods:edit,mall:store-goods:add')")
    @PostMapping("/allList")
    public HttpResult<List<MallGoodsNorms>> allList(@RequestBody MallGoodsNorms mallGoodsNorms) {
        List<MallGoodsNorms> list = mallGoodsNormsService.lambdaQuery()
                .eq(MallGoodsNorms::getGoodsId, mallGoodsNorms.getGoodsId())
                .orderByAsc(MallGoodsNorms::getSortOrder).list();
        return HttpResult.ok(list);
    }

    @PostMapping("/allListByAdmin")
    public HttpResult<List<MallGoodsNorms>> allListByAdmin(@RequestBody MallGoodsNorms mallGoodsNorms) {
        List<MallGoodsNorms> list = mallGoodsNormsService.lambdaQuery()
                .eq(MallGoodsNorms::getGoodsId, mallGoodsNorms.getGoodsId())
                .orderByAsc(MallGoodsNorms::getSortOrder).list();
        return HttpResult.ok(list);
    }

    /**
     * 导出商品规格参数列表
     */
    @ApiOperation(value="导出商品规格参数列表")
    @PreAuthorize("@ss.hasPermi('system:norms:export')")
    @Log(title = "商品规格参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallGoodsNorms mallGoodsNorms) {
        List<MallGoodsNorms> list = mallGoodsNormsService.selectMallGoodsNormsList(mallGoodsNorms);
        ExcelUtil<MallGoodsNorms> util = new ExcelUtil<MallGoodsNorms>(MallGoodsNorms.class);
        return util.exportExcel(list, "norms");
    }

    /**
     * 获取商品规格参数详细信息
     */
    @ApiOperation(value="获取商品规格参数详细信息")
    @PreAuthorize("@ss.hasPermi('system:norms:query,mall:store-goods:edit')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallGoodsNorms> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallGoodsNormsService.selectMallGoodsNormsById(id));
    }

    /**
     * 新增商品规格参数
     */
    @ApiOperation(value="新增商品规格参数")
    @PreAuthorize("@ss.hasPermi('system:norms:add,mall:store-goods:edit')")
    @Log(title = "商品规格参数", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallGoodsNorms mallGoodsNorms) {
        mallGoodsNorms.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return to(mallGoodsNormsService.insertMallGoodsNorms(mallGoodsNorms));
    }

    /**
     * 修改商品规格参数
     */
    @ApiOperation(value="修改商品规格参数")
    @PreAuthorize("@ss.hasPermi('system:norms:edit,mall:store-goods:edit')")
    @Log(title = "商品规格参数", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallGoodsNorms mallGoodsNorms) {
        return to(mallGoodsNormsService.updateMallGoodsNorms(mallGoodsNorms));
    }

    /**
     * 删除商品规格参数
     */
    @ApiOperation(value="删除商品规格参数")
//    @PreAuthorize("@ss.hasPermi('system:norms:remove,mall:store-goods:edit')")
    @Log(title = "商品规格参数", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallGoodsNormsService.deleteMallGoodsNormsByIds(ids));
    }


    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallGoodsNorms
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Integer> reSortOrderReduce(@RequestBody MallGoodsNorms mallGoodsNorms){
        return HttpResult.ok(mallGoodsNormsService.reSortOrderReduce(mallGoodsNorms));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallGoodsNorms
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Integer> reSortOrderPlus(@RequestBody MallGoodsNorms mallGoodsNorms){
        return HttpResult.ok(mallGoodsNormsService.reSortOrderPlus(mallGoodsNorms));
    }


}
